package com.hundsun.yr.universal.p2p.presenter.impl;

import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.data.HSSharedPreferences;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.library.widget.HSToast;
import com.hundsun.yr.universal.p2p.bean.ConfigBeen;
import com.hundsun.yr.universal.p2p.bean.UserInfo;
import com.hundsun.yr.universal.p2p.config.Config;
import com.hundsun.yr.universal.p2p.config.Constants;
import com.hundsun.yr.universal.p2p.config.TaskConstants;
import com.hundsun.yr.universal.p2p.interactor.base.RequestInteractor;
import com.hundsun.yr.universal.p2p.interactor.base.SplashInteractor;
import com.hundsun.yr.universal.p2p.interactor.impl.SplashInteractorImpl;
import com.hundsun.yr.universal.p2p.interactor.impl.StringRequestGetInteractorImpl;
import com.hundsun.yr.universal.p2p.listener.BaseMultiLoadedListener;
import com.hundsun.yr.universal.p2p.presenter.base.SplashPresenter;
import com.hundsun.yr.universal.p2p.ui.activity.SplashActivity;
import com.hundsun.yr.universal.p2p.utils.BaseRequestTool2;
import com.hundsun.yr.universal.p2p.utils.Logs;
import com.umeng.socialize.utils.Log;

import org.json.JSONObject;

import java.util.HashMap;

public class SplashPresenterImpl implements SplashPresenter, BaseMultiLoadedListener<String> {

    private Context mContext = null;
    private SplashActivity mSplashView = null;
    private SplashInteractor mSplashInteractor = null;
    private HSSharedPreferences hsSharedPreferences = null;
    /**
     * 请求接口
     */
    private RequestInteractor mInteractor = null;

    public SplashPresenterImpl(Context context, SplashActivity splashView) {
        if (null == splashView) {
            throw new IllegalArgumentException("Constructor's parameters must not be Null");
        }

        mContext = context;
        mSplashView = splashView;
        mSplashInteractor = new SplashInteractorImpl();
        hsSharedPreferences = new HSSharedPreferences(context, Config.SPPath.file_userLoginedInfo);
        mInteractor = new StringRequestGetInteractorImpl(this);
    }

    @Override
    public void initialized() {
        // TODO Auto-generated method stub

        mSplashView.initializeViews(mSplashInteractor.getVersionName(mContext),
                mSplashInteractor.getCopyright(mContext),
                mSplashInteractor.getBackgroundImageResID());
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                mSplashView.navigateToHomePage(hsSharedPreferences.getBoolean(UserInfo.ISLOGIN));
            }
        }, 3000);
    }

    /**
     * 进行第一个Url的请求，拿到key并且进行保存
     */
    @Override
    public void config() {
        String zwgt_data = BaseRequestTool2.getFormatedDateString();
        loadingData(TaskConstants.TASK_CONFIG, mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_index_test) + TaskConstants.TASK_CONFIG,
                Constants.EVENT_DATA,
                BaseRequestTool2.getHearder(mContext, "get", mContext.getString(R.string.root_url_test) + mContext.getString(R.string.api_index_test) + TaskConstants.TASK_CONFIG, "", "key", zwgt_data), false);
    }

    @Override
    public void loadingData(String task_name, String url, int event_tag, HashMap<String, String> params, boolean isSwipeRefresh) {
        //进行真正的网络请求
        if (NetUtils.isNetworkConnected(mContext)) {
            mInteractor.getRequestData(task_name, url, event_tag, params);
        } else {
            new HSToast(mContext).showToast(R.string.common_no_network);
        }
    }

    @Override
    public void loadingData(String task_name, String url, int event_tag, HashMap<String, String> params, HashMap<String, String> header, boolean isSwipeRefresh) {

    }

    @Override
    public void loadingData(String task_name, String url, int event_tag, JSONObject params, HashMap<String, String> header, boolean isSwipeRefresh) {

    }

    /**
     * 网络请求成功的回调,对key进行存储
     *
     * @param task_name
     * @param event_tag
     * @param data
     */
    @Override
    public void onSuccess(String task_name, int event_tag, String data) {
        Logs.e("网络请求成功data: " + data);
        //将的到的key保存下来
        try {
            Gson gson = new Gson();
            ConfigBeen configBeen = gson.fromJson(data, ConfigBeen.class);
            String code = configBeen.code;
            //先拿到code里面记录了请求的结果
            if (TextUtils.equals(code, Constants.REQUESTCODE_000000)) {
                //将config里面的key数据存到名字为p2p_config的sp里面
                mSplashView.mConfigSP.putShareProf("config", configBeen.data.key);
                mSplashView.mConfigSP.putShareProf("server_version", configBeen.data.version);
                mSplashView.mConfigSP.putShareProf("min_version", (String) configBeen.data.andorid_minversion);
            }else {
                //如果访问失败就给他一个固定的key
                mSplashView.mConfigSP.putShareProf("config", mContext.getString(R.string.default_key));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 网络请求失败的回调
     *
     * @param task_name
     * @param msg
     */
    @Override
    public void onException(String task_name, String msg) {
        Logs.e("网络请求失败msg: " + msg);
    }
}
